package cn.org.xiaosheng.core.config;

import cn.org.xiaosheng.core.protocol.Serializer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 序列化配置
 * @author XiaoSheng
 * @date 2024/8/20 下午9:35
 */
public abstract class Config {

    public static final String LOCAL_ADDRESS = "127.0.0.1";
    public static final String NACOS_PORT = "8848";

    static Properties properties;
    static {
        try (InputStream in = Config.class.getResourceAsStream("/application.properties")) {
            properties = new Properties();
            properties.load(in);
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    public static int getServerPort() {
        String value = properties.getProperty("server.port");
        if(value == null) {
            return 8080;
        } else {
            return Integer.parseInt(value);
        }
    }
    public static Serializer.Algorithm getSerializerAlgorithm() {
        String value = properties.getProperty("rpc.serializer.algorithm");
        if(value == null) {
            return Serializer.Algorithm.Java;
        } else {
            return Serializer.Algorithm.valueOf(value);
        }
    }

    public static String getNacosAddress() {
        String address = properties.getProperty("rpc.nacos.address");
        if (address == null || "".equals(address)) {
            return LOCAL_ADDRESS;
        } else {
            return address;
        }
    }

    public static String getNacosPort() {
        String port = properties.getProperty("rpc.nacos.port");
        if (port == null || "".equals(port)) {
            return NACOS_PORT;
        } else {
            return port;
        }
    }

}
